﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDxfDocument

{
    class ISYC_308_07
    {

        //图F.89  ISYC_308_07-可转位刀片心轴连接三面刃轮廓槽铣刀
        public static void ISYC_308_07_j(Dictionary<string, Object> data)  //  可转位刀片心轴连接三面刃轮廓槽铣刀
        {

            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan }; //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
           // double sl_ = Convert.ToDouble(data["oal"]);
            double scale = Convert.ToDouble(data["scale"]);
            // double sl_ = 45;
            /* double scale = 1;

             double cw = 13;
               double dc = 90.5;
               double lta = 30.7;
               double oal = 31.2;
               double dhub = 27.66;
               double dconms = 13.83;
               double kapr = 90;
               double kapr_ = kapr * pi / 180;*/
            //Convert.ToDouble(data[""])
            double cw = Convert.ToDouble(data["cw"]);
            double dc = Convert.ToDouble(data["dc"]);
            double lta = Convert.ToDouble(data["lta"]);
            double oal = Convert.ToDouble(data["oal"]);
            double dhub = Convert.ToDouble(data["dhub"]);
            double dconms = Convert.ToDouble(data["dconms"]);
            double kapr = Convert.ToDouble(data["kapr"]);
            double kapr_ = kapr * pi / 180;
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p0_1 = fun.Polar(p0, pi, lta + 5);
            Vector2 p0_2 = fun.Polar(p0, 0, lta + 5);

            Vector2 p1 = fun.Polar(p0, pi / 2, dc / 2.0 - dc / 40.0);
            Vector2 p2 = fun.Polar(p0, pi / -2 - 0.0001 * pi / 180, dc / 2.0 - dc / 40.0);
            Vector2 p3_1 = fun.Polar(p1, pi / 2 + kapr_ + 15 * pi / 180, dc);
            Vector2 p3_2 = fun.Polar(p0, pi, cw - 3.15 / 13 * cw);
            Vector2 p3_3 = fun.Polar(p3_2, pi / 2 + 0.0001 * pi / 180, dc);
            Vector2 p3 = fun.Inters(p3_2, p3_3, p1, p3_1);
            Vector2 p4 = fun.mirrorpoint(p3, p0_1, p0_2);
            Vector2 p5_0 = fun.Polar(p0, 0, oal - cw);
            Vector2 p5 = fun.Polar(p5_0, pi / 2, dhub / 2.0);

            Vector2 p0_3 = fun.Polar(p5, pi / -2, dhub / 4.0);
            Vector2 p0_4 = fun.Polar(p0_3, pi, lta);
            Vector2 p0_5 = fun.Polar(p5, pi / -2, dhub / 4.0 * 3);
            Vector2 p0_6 = fun.Polar(p0_5, pi, lta);

            Vector2 p6_0 = fun.Polar(p5, pi, dc);
            Vector2 p6 = fun.Inters(p1, p2, p5, p6_0);
            Vector2 p7 = fun.Inters(p3, p4, p5, p6_0);
            Vector2 p8 = fun.Polar(p7, pi, 2 / 31.2 * oal - 1 / 31.2 * oal);
            Vector2 p9 = fun.Polar(p8, pi + pi / 4, Math.Sqrt(Math.Pow(1 / 31.2 * oal, 2) + Math.Pow(1 / 27.66 * dhub, 2)));
            Vector2 p10 = fun.mirrorpoint(p9, p0_1, p0_2);
            Vector2 p11 = fun.mirrorpoint(p8, p0_1, p0_2);
            Vector2 p12 = fun.mirrorpoint(p7, p0_1, p0_2);
            Vector2 p13 = fun.mirrorpoint(p6, p0_1, p0_2);
            Vector2 p14 = fun.mirrorpoint(p5, p0_1, p0_2);
            Vector2 p15 = fun.Inters(p3, p4, p0_1, p0_2);
            Vector2 p16_0 = fun.Polar(p15, pi / 2, 0.27 / 90.5 * dc);
            Vector2 p17 = fun.Polar(p16_0, pi + 15 * pi / 180, fun.CalDistance(p15, p16_0) * 2.5);
            Vector2 p18 = fun.Polar(p17, (pi * 2 - fun.CalAngle(p4, p2)) / 2, fun.CalDistance(p17, p16_0) * 20);
            Vector2 p16 = fun.Inters(p16_0, p15, p17, p18);
            Vector2 p19 = fun.Polar(p17, fun.CalAngle(p17, p18) + 77 * pi / 180, fun.CalDistance(p17, p16_0) * 4);
            Vector2 p20_1 = fun.Polar(p17, fun.CalAngle(p17, p18), fun.CalDistance(p17, p18) / 2.0);
            Vector2 p20_2 = fun.Polar(p20_1, fun.CalAngle(p17, p18) + pi / 2, dc);
            Vector2 p20 = fun.mirrorpoint(p19, p20_1, p20_2);
            Vector2 p21 = fun.Polar(p20_1, fun.CalAngle(p17, p18), fun.CalDistance(p15, p16_0) / 1.5);
            Vector2 p22_0 = fun.Polar(p21, fun.CalAngle(p18, p17) - 77 * pi / 180, dc);
            Vector2 p22 = fun.Inters(p19, p20, p22_0, p21);
            Vector2 p23 = fun.mirrorpoint(p22, p20_1, p20_2);
            Vector2 p24 = fun.Polar(p20_1, fun.CalAngle(p18, p17), fun.CalDistance(p15, p16_0) / 1.5);

            Vector2 p25_0 = fun.Inters(p0_1, p0_2, p1, p2);
            Vector2 p25_1 = fun.Polar(p25_0, pi / 2, dc);
            Vector2 p25 = fun.Inters(p17, p18, p25_0, p25_1);
            Vector2 p26 = fun.Inters(p23, p20, p1, p2);
            Vector2 p27 = fun.Inters(p3, p4, p19, p22);
            Vector2 p28_0 = fun.Polar(p4, 0, 2.23 / 13 * cw);
            Vector2 p28 = fun.Polar(p28_0, pi / 2, 2.23 / 13 * cw);
            Vector2 p29 = fun.Polar(p2, fun.CalAngle(p4, p2), Math.Sqrt(Math.Pow(1.43 / 13 * cw, 2) + Math.Pow(1.62 / 90.5 * dc, 2)));
            Vector2 p30 = fun.Polar(p29, pi, cw * 2.0 / 3.0);
            Vector2 p31 = fun.Polar(p30, pi / 3, cw * 2 / 3.0);
            Vector2 a = fun.Polar(p28, fun.CalAngle(p30, p31) - pi / 2, dc);
            Vector2 b = fun.Inters(p30, p31, p28, a);
            Circle circle1 = new Circle(Pus.ToV3(p28, 0), fun.CalDistance(p28, b));//圆
            Vector2 p32 = fun.mirrorpoint(p28, p0_1, p0_2);
            Vector2 p33 = fun.mirrorpoint(p29, p0_1, p0_2);
            Vector2 p34 = fun.mirrorpoint(p30, p0_1, p0_2);
            Vector2 p35 = fun.mirrorpoint(p31, p0_1, p0_2);
            Circle circle2 = new Circle(Pus.ToV3(p32, 0), circle1.Radius);//圆
            Circle circle3 = new Circle(Pus.ToV3(p32, 0), circle1.Radius / 4.0);
            Vector2 p36 = fun.Polar(p28, pi / 2, circle1.Radius);
            Vector2 p37 = fun.Polar(p28, pi / -2 - 1 * pi / 180, circle1.Radius);
            Arc arc1 = fun.DrawArc(p36, p28, p37);
            Vector2 p38 = fun.Inters1(fun .Polar (p3,pi,0.01), p4, arc1, 0);
            Vector2 p39 = fun.Polar(p28, pi / -2 + 1 * pi / 180, circle1.Radius);
            Arc arc2 = fun.DrawArc(p39, p28, p36);
            Vector2 p40 = fun.Inters1(p2, p4, arc2, 1);
            Vector2 p41 = fun.Inters(p2, p4, p30, p31);
            Vector2 p42 = fun.Inters(p1, p2, p31, p29);

            Vector2 p43 = fun.mirrorpoint(p28, p0_1, p0_2);
            Vector2 p44 = fun.mirrorpoint(p40, p0_1, p0_2);
            Vector2 p45 = fun.mirrorpoint(p41, p0_1, p0_2);
            Vector2 p46 = fun.mirrorpoint(p42, p0_1, p0_2);
            Vector2 p47 = fun.mirrorpoint(p38, p0_1, p0_2);

            Vector2 p48 = fun.Polar(p26, pi / 2, 1.62 / 90.5 * dc);
            Vector2 p49_0 = fun.Polar(p48, pi / 2, 4.99 / 90.5 * dc);
            Vector2 p49 = fun.Polar(p49_0, pi, 3.94 / 31.2 * oal);
            Vector2 p50 = fun.Polar(p49, pi / 2, 3.948 / 90.5 * dc);
            Vector2 p51 = fun.Polar(p7, pi / 2, 3.9 / 90.5 * dc);

            Vector2 p52 = fun.Polar(p32, pi / 2, arc1.Radius);
            Vector2 p53 = fun.Polar(p32, pi / -2 - 0.001 * pi / 180, arc1.Radius);
            Vector2 p54 = fun.Polar(p32, pi / -2 + 0.001 * pi / 180, arc1.Radius);

            Vector2 p55 = fun.Polar(p32, pi / 2, circle1.Radius / 4.0);
            Vector2 p56 = fun.Polar(p32, pi / -2 - 0.001 * pi / 180, circle1.Radius / 4.0);
            Vector2 p57 = fun.Polar(p32, pi / -2 + 0.001 * pi / 180, circle1.Radius / 4.0);
            Arc arc3 = fun.DrawArc(p38, p28, p40);
            Arc arc4 = fun.DrawArc(p52, p32, p53);
            Arc arc5 = fun.DrawArc4(p57, circle3.Radius, p56, 1);
            Arc arc7 = fun.DrawArc(p54, p32, p52);

            Line line1 = new Line(p40, p41);
            var polyline1 = Pus.AddPointsToLwPolyLine(p41, p30, p29, p42);
            var poluline2 = Pus.AddPointsToLwPolyLine(p34, p35, p33, p34);
            var poluline3 = Pus.AddPointsToLwPolyLine(p47, p27, p19, p17, p16, p4, p2, p25, p18, p20, p26, p46, p35, p45, p44);//注
            var poluline4 = Pus.AddPointsToLwPolyLine(p18, p17, p19, p22, p21, p24, p23, p20, p18);
            var poluline5 = Pus.AddPointsToLwPolyLine(p7, p8, p9, p10, p11, p12, p4, p2, p13, p14, p5, p6);
            var poluline6 = Pus.AddPointsToLwPolyLine(p48, p49, p50, p51);
            poluline5.Vertexes[1].Bulge = Math.Tan((pi / 2) / 4);
            poluline5.Vertexes[3].Bulge = Math.Tan((pi / 2) / 4);
            poluline6.Vertexes[0].Bulge = -Math.Tan((83 * pi / 180) / 4);
            poluline6.Vertexes[2].Bulge = Math.Tan((127 * pi / 180) / 4);
            
            dxf.AddEntity(new EntityObject[] { polyline1 ,poluline2 ,circle2,circle3,poluline3 ,poluline4 ,poluline5,poluline6
            ,arc3
            ,line1
            });
            fun.PolyFill(dxf, polyline1, 0.5, contour);
            fun.PolyFill(dxf, poluline2, 0.5, contour);
            fun.PolyFill(dxf, poluline3, 0.5, contour);
            fun.PolyFill(dxf, poluline4, 0.5, contour);
            fun.PolyFill(dxf, poluline5, 0.5, contour);
            fun.PolyFill(dxf, poluline6, 0.5, contour);
            fun.drawBArc(dxf, arc3, 0.5, contour);
            fun.drawBArc(dxf, arc4, 0.5, contour);
            fun.drawBArc(dxf, arc5, 0.5, contour);
            fun.drawBArc(dxf, arc7, 0.5, contour);
            Line line2 = new Line(p0_1, p0_2);
            line2.Lineweight = Lineweight.W25;//Lineweight指线宽
            line2.Layer = centre;//Layer指图层
            line2.LinetypeScale = 5;//指线性比例
            line2.Linetype = Linetype.DashDot;//Linetype指是点划线还是虚线什么的
            dxf.AddEntity(line2); //提取

            Line line3 = new Line(p0_3, p0_4);
            line3.Lineweight = Lineweight.W35;//Lineweight指线宽
            line3.Layer = refin;//Layer指图层
            line3.LinetypeScale = 5;//指线性比例
            line3.Linetype = Linetype.Dashed;//Linetype指是点划线还是虚线什么的
            dxf.AddEntity(line3); //提取
            Line line4 = new Line(p0_5, p0_6);
            line4.Lineweight = Lineweight.W35;//Lineweight指线宽
            line4.Layer = refin;//Layer指图层
            line4.LinetypeScale = 5;//指线性比例
            line4.Linetype = Linetype.Dashed;//Linetype指是点划线还是虚线什么的
            dxf.AddEntity(line4); //提取

            //标注
            
            Vector2 v1 = fun.Polar(p32, pi, arc1.Radius);
            Vector2 v2 = fun.Polar(p28, pi, arc1.Radius);
            Vector2 v3 = fun.Polar(p51, pi / -2, fun.CalDistance(p51, p7));
            Vector2 v4 = fun.Polar(v1, pi / 2, 0.1);

            Vector2 v5 = fun.Polar(p0_5, 0, 3);
            Vector2 v6 = fun.Polar(v5, pi / -3, cw / 2.0);
            Vector2 v7 = fun.Polar(v6, pi / -3, cw / 2.0);
            Vector2 v8 = fun.Polar(p32, pi - pi / 4, oal);
            Vector2 v9 = fun.Polar(v8, pi / -2, cw / 2.0);
            Vector2 v10 = fun.Polar(v9, pi / -2, cw / 2.0);
            Vector2 v11 = fun.Polar(p5, pi / -2, dhub / 2.0);
            Vector2 v12 = fun.Polar(p9, pi / -2, fun.CalDistance(p9, p10) / 2.0);
            Vector2 v13 = fun.Polar(v11, pi / -2 - 10 * pi / 180, 0.5 * 1.7 * scale);
            Vector2 v14 = fun.Polar(v13, pi / -2 - 10 * pi / 180, dhub / 1.5);
            Vector2 v15 = fun.Polar(v14, pi / -2, 1.2 * scale);
            Vector2 v16 = fun.Polar(v12, pi + 80 * pi / 180, 0.5 * 1.7 * scale);
            Vector2 v17 = fun.Polar(v16, pi + 80 * pi / 180, dhub / 1.5);
            Vector2 v18 = fun.Polar(v17, pi / -2, 1.2 * scale);

            Vector2 v19 = fun.Polar(v1, pi / 2, dc);
            Vector2 v20 = fun.Polar(p34, pi, dc);
            Vector2 v21 = fun.Inters(v1, v19, v20, p34);
            Vector2 v22 = fun.Polar(v21, pi / 2 + pi / 4, 0.5 * 1.7 * scale);
            Vector2 v23 = fun.Polar(v22, pi / 2 + pi / 4, cw);

            fun.dimrot1(dxf, p5, v1, 0, oal, 2, 1.2 * scale, 1.6 * scale, 0.9, "LTA,LF", "", "");
            fun.dimrot1(dxf, p5, p9, 0, oal * 1.8, 2, 1.2 * scale, 1.6 * scale, 0.9, "OAL,THUB,LPR", "", "");
            fun.dimrot1(dxf, p29, v2, 180, oal / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "CW,APMX", "", "");
            fun.dimrot1(dxf, p33, p29, 90, oal * 1.5, 2, 1.2 * scale, 1.6 * scale, 0.9, "DC", "", "");
            fun.dimrot1(dxf, p33, v3, 90, oal, 2, 1.2 * scale, 1.6 * scale, 0.9, "CDX", "", "");
            fun.dimrot1(dxf, p0_3, p0_5, -90, oal, 2, 1.2 * scale, 1.6 * scale, 0.9, "DCONMS", "", "");
            fun.dimrot1(dxf, p5, p14, -90, oal * 1.5, 2, 1.2 * scale, 1.6 * scale, 0.9, "DHUB", "", "");
            fun.jdbz2(dxf, new Line(p34, p33), new Line(v1, v4), oal / 3.0, 1.2 * scale, 1.6 * scale, 0.9, "KAPR", "", "");
            fun.Singlelinearrow(dxf, "CCMS", v5, -60, v6, v7, 1.2 * scale, 1.6 * scale);
            /*fun.Singlelinearrow(dxf, "FDP", v8, 90, v9, v10, 1.2 * scale, 1.6 * scale);
            fun.Axis(dxf, fun.Polar(p14, pi / -3, oal), 0, -90, "Z", "X", "Y", 0.5, 1.2 * scale, 0);
            fun.Axis(dxf, v11, 0, -90, "", "", "", 0.5, 1.2 * scale, 0);
            fun.Axis(dxf, v12, 0, -90, "", "", "", 0.5, 1.2 * scale, 0);*/
            Line linev1 = new Line(v13, v14);
            Text txt1 = new Text("PCS,MCS", v15, 1.2 * scale, 0);
            Line linev2 = new Line(v16, v17);
            Text txt2 = new Text("CIP", v18, 1.2 * scale, 0);
            Line linev3 = new Line(v22, v23);
            Text txt3 = new Text("CRP", v23, 1.2 * scale, 0);
            Circle circle4 = new Circle(Pus.ToV3(v22, 0), 0.5 * 1.7 * scale);//圆
            /* dxf.AddEntity(new EntityObject[]{ linev1,linev2 ,linev3
                 ,txt1,txt2  ,txt3
                 ,circle4
             });*/


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}


